<?php

/**
 * MVM_MALL 网上商店系统  商品搜索
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-03-01 $
 * $Id: search.php  www.mvmmall.com$
 * ---------------------------------------------
*/
require_once './include/common.inc.php';
require_once MVMMALL_ROOT.'header.php';
if($action!='search'){
    require_once MVMMALL_ROOT.'./include/pager.class.php';
	$search_key  = '';
	if (isset($ps_search))
	{
		$search_arr = array();
		if (stristr($ps_search, 'AND') !== false) {
			$search_arr = explode('AND', $ps_search);//检查AND
			$compopr    = " AND ";
		} elseif (stristr($ps_search, 'OR') !== false) {
			$search_arr = explode('OR', $ps_search);//检查OR
			$compopr    = " OR ";
		} elseif (stristr($ps_search, '+') !== false || stristr($ps_search, '＋') !== false) { //检查+有全角和半角的分别
			$search_arr = explode('+', $ps_search);
			$search_arr = explode('＋', $ps_search);
			$compopr    = " AND ";
		}else {
			$search_arr = explode(' ', $ps_search);//检查空格有全角和半角
			$search_arr = explode('　', $ps_search);//检查空格
			$compopr    = " OR ";
		}
		$search_key = 'AND (';
		$search_key .= $ps_code ? "(goods_code LIKE '%" .filtering($ps_code)."%') AND ": '';//编码搜索
		$tag_ids = array();
		foreach ($search_arr AS $key => $val) {
			if ($key > 0 && $key < count($search_arr) && count($search_arr) > 1) {
				$search_key .= $compopr;
			}
			//内容转义
			$val            = filtering($val);
			$str_advance    = $goods_advance ? " OR goods_main LIKE '%$val%'" : '';
			$search_key    .= "(goods_name LIKE '%$val%' OR goods_key LIKE '%$val%' $str_advance)";
			$result = $db->query("SELECT DISTINCT tag_id,goods_id,tag_name FROM `{$tablepre}tag`  WHERE tag_name LIKE '%$val%'");
			while ($row = $db->fetch_array($result)) {
				$tag_ids[] = $row['goods_id'];
			}
			//写入关键字数据库
			if ($val!=''){
				$m_check_id ?$m_check_id :'Guest';
				$query = "INSERT INTO `{$tablepre}search_table` SET
                      search_id         = '$m_check_id',
                      search_category   = '$ps_ctid',
                      search_word       = '$val',
                      register_date     = '$m_now_time'";
				$db->query($query);
			}
		}
		$search_key .= ')';
	}

	//商品标签搜索
	$tag_ids    = array_unique($tag_ids);
	$tag_search = implode(',',dhtmlchars($tag_ids));
	$tag_search && $tag_search = "OR uid IN($tag_search)";
	//分类搜索
	if($ps_ctid) {
		$cat_list   = $db->get_one("SELECT uid FROM `{$tablepre}category` WHERE uid = '$ps_ctid'");
		!$cat_list['uid'] && show_msg('ctegory_error');
		require_once MVMMALL_ROOT.'./include/category_tree.class.php';
		$tree           = new tree();
		$tree->arr_tree = $cache->get_cache('tree');
		//多重分类
		$rt_cat    = false;
		$result    = $db->query("SELECT * FROM {$tablepre}goods_cat WHERE cat_id='$cat_list[uid]'");
		while ($rt = $db->fetch_array($result)) {
			$rt_cat[$rt['goods_id']] = $rt['goods_id'];
		}
		$goods_cat   = $rt_cat ? 'OR uid IN('.implode(",", $rt_cat).')': false;
		//end
		$arr = implode(',',$tree->get_id($ps_ctid));
		$cat_in = $arr ? $ps_ctid.','.$arr: $ps_ctid;

		$cat_search = "  AND goods_category IN($cat_in) $goods_cat";
	}
	$brand_search          = $ps_brand ? ' AND goods_brand=' .intval($ps_brand): '';//品牌搜索
	//价格搜索
	$max_search            =  $ps_max_money != 0 || $ps_max_money < 0 ? ' AND goods_sale_price <=' .floatval($ps_max_money): '';
	$min_search            =  $ps_min_money != 0  ? ' AND goods_sale_price >=' .floatval($ps_min_money): '';

	//生成搜索的sql
	$search_sql            = false;
	$search_sql            = "WHERE upv = '1'  AND up_date<='$m_now_time'"." AND (( 1 " . $cat_search . $search_key . $brand_search . $min_search . $max_search ." ) ".$tag_search." )";
	$total_count           = $db->counter("{$tablepre}goods_table",$search_sql);
	$page                  = $page ? (int)$page:1;
	$list_num              = $mm_limit_list;//后台可控制显示数量
	$rowset                = new Pager($total_count,$list_num,$page);
	$from_record           = $rowset->_offset();
	$order_by              = " ORDER BY goods_rank ASC ";
	$query                 =  "SELECT * FROM `{$tablepre}goods_table` $search_sql $order_by limit $from_record, $list_num";
	$result                = $db->query($query);
	while($list  = $db->fetch_array($result)) {
		$goods[] = goods_array($list);
	}
	$baseurl       = "search.php?action=$action&ps_ctid=$ps_ctid&ps_search=$ps_search&ps_company=$ps_company&ps_brand=$ps_brand&ps_min_money=$ps_min_money&ps_max_money=$ps_max_money&ps_line=$ps_line&page=";
	$page_list     = $rowset->link($baseurl);
	$navigation    = make_navigation($lang['search_result'],$baseurl.$page);//导航
}else {
	$brand_select  = drop_menu($cache->get_cache('brand'),'ps_brand');
	$category_list = cat_menu('ps_ctid');
	$navigation    = make_navigation($lang['sitemap_search'],GetBaseUrl('search','search'));//导航
}
require_once template('search');
footer();